package com.justech.mes.v2.dao.mapper;

import com.justech.mes.v2.model.MesUserAbnormalTimeRecord;
import com.justech.mes.v2.model.response.MesOperationAbnormalResponse;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @author feifei.li
 */
@Mapper
public interface MesUserAbnormalTimeRecordMapper {

    /**
     * 获取最早发生的无效异常(未被解除)
     *
     * @param userName
     * @return
     */
    @Select("SELECT * FROM `mes_v2_user_abnormal_time_record` WHERE end_time IS NULL AND STATUS=0 AND user_name=#{userName} ORDER BY create_time asc LIMIT 1")
    MesUserAbnormalTimeRecord findInvalidFirstAbnormal(@Param("userName") String userName);

    /**
     * 获取用户身上的异常
     *
     * @param userName
     * @return
     */
    @Select("SELECT * FROM `mes_v2_user_abnormal_time_record` WHERE end_time IS NULL AND  TYPE IS NULL AND user_name=#{userName} ORDER BY create_time asc")
    List<MesUserAbnormalTimeRecord> findAbnormalsByUser(@Param("userName") String userName);

    /**
     * 获取用户的有效异常
     *
     * @param userName
     * @return
     */
    @Select("SELECT * FROM mes_v2_user_abnormal_time_record WHERE user_name=#{userName} AND end_time IS NULL AND STATUS=1")
    List<MesUserAbnormalTimeRecord> findEffevtiveAbnormal(@Param("userName") String userName);

    /**
     * 获取未被解除的异常
     *
     * @param param
     * @return
     */
    List<MesOperationAbnormalResponse> findUnreleasedAbnormalGroup(@Param("param") MesUserAbnormalTimeRecord param);

    /**
     * 查询用户名下未被解除的异常
     *
     * @param userName
     * @return
     */
    @Select("SELECT * FROM `mes_v2_user_abnormal_time_record` WHERE user_name=#{userName} AND end_time IS NULL AND STATUS=1")
    List<MesUserAbnormalTimeRecord> findUnliftedAbnormalByUserName(@Param("userName") String userName);

    /**
     * 筛选异常记录
     *
     * @param params
     * @return
     */
    List<MesUserAbnormalTimeRecord> findByParams(@Param("params") Map<String, Object> params);

    /**
     * 获取用户名下未被解除的异常
     *
     * @param userName
     * @return
     */
    List<MesUserAbnormalTimeRecord> findUnreleasedAbnormalByUser(@Param("userName") String userName);

    /**
     * 获取固定异常
     *
     * @param procDate 排程日期
     * @param batchId  班次id
     * @param userName 用户工号
     * @return
     */
    @Select("SELECT * FROM `mes_v2_user_abnormal_time_record` WHERE proc_date=#{procDate} AND TYPE=1 AND batch_id=#{batchId} AND user_name=#{userName}")
    MesUserAbnormalTimeRecord findFixedAbnormal(@Param("procDate") String procDate, @Param("batchId") Long batchId, @Param("userName") String userName);

    /**
     * 根据异常ID查询被异常影响到的工时记录
     * <p>
     * 结束时间做截取
     * </p>
     *
     * @param param
     * @return
     */
    List<MesUserAbnormalTimeRecord> findbyAbnormalUuid(@Param("param") Map<String, Object> param);
}
